//
//  jwTableViewController.m
//  PASystem
//
//  Created by michael on 16/9/21.
//  Copyright © 2016年 czd. All rights reserved.
//
#import "chaxunzhixunViewController.h"

#import "jwTableViewController.h"
#import "jwTableViewCell.h"
#import "jwxqTableViewController.h"
@interface jwTableViewController ()
@property(nonatomic,assign)int currentPage; //当前页
@property(nonatomic,strong)NSMutableArray * dataArray;
@property(nonatomic,assign)int totalCounts; //总条数
@end

@implementation jwTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = _string;
    self.view.backgroundColor = [UIColor whiteColor];
    
//    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0,0, KScreenWidth, KScreenHeight-64-46) style:UITableViewStylePlain];
//     self.tableView.delegate = self;
//    self.tableView.dataSource =self;

//    [self.view addSubview:_tableView];
 __weak typeof(self) weakSelf = self;
    [self.tableView addHeaderWithCallback:^{
    weakSelf.currentPage = 1;

        [weakSelf loadData]; //加载网络数据
    }];
    [self.tableView headerBeginRefreshing];
    
    [self.tableView addFooterWithCallback:^{
                     [weakSelf loadMore];
       
       
    }];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"search"]  style:UIBarButtonItemStylePlain target:self action:@selector(rightButtonAction)];
        [self.navigationItem setRightBarButtonItem:rightButton];

    
}
- (void)rightButtonAction{

chaxunzhixunViewController *vc  = [[chaxunzhixunViewController alloc]init];
vc.type = _type;
vc.string = _string;
vc.service = _service;
vc.action = _action;
vc.type = _type;
[self.navigationController pushViewController:vc animated:YES];


}

    

#pragma mark 刷新数据
-(void)refreshData{
    
    self.currentPage = 1;
    [self loadData]; //加载网络数据
}

-(NSMutableArray*)dataArray{
    if (_dataArray == nil) {
      _dataArray =   [NSMutableArray array];
    }
    return _dataArray;
}
#pragma mark 加载网络数据
-(void)loadData{
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
 NSString *json =[NSString stringWithFormat: @"{'action':%@,'service':%@, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld,type:%@,name:%@,endtime:%@,begintime:%@}'}",_action,_service,userId,pwdId,userHumber,(long)self.currentPage,_type,_name,_timeafterstring,_timebeforestring];
     NSArray * array = @[@{@"arg0":json}];
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {
if (self.currentPage ==1) {
            [self.dataArray removeAllObjects];
        }
       
     NSArray *array = JSON[@"reason"];
        for (NSDictionary *dic in array) {
    NSDictionary *dict = dic[@"attrs"];
//    NSLog(@"%@",dict);
    [_dataArray addObject:dict];
    
}
//            _totalCounts =  _dataArray.count;
        [self.tableView reloadData];
        [self.tableView headerEndRefreshing];
        self.currentPage++;
        if (self.dataArray.count ==0) {
        [self.view makeToast:@"暂时还没有数据哦"];
        }
    } failure:^(NSError *error) {
     [self.tableView headerEndRefreshing];
        
    }];
 
}
#pragma mark 加载更多
-(void)loadMore{
   NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSString * pwdId = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];NSString * userHumber = [[NSUserDefaults standardUserDefaults]objectForKey:@"userHumber"];
    
 NSString *json =[NSString stringWithFormat: @"{'action':%@,'service':%@, 'userName':'%@','password':'%@', 'vaueJson':'{userid :%@,pageNum:%ld,type:%@,name:%@,endtime:%@,begintime:%@}'}",_action,_service,userId,pwdId,userHumber,(long)self.currentPage,_type,_name,_timeafterstring,_timebeforestring];
      NSArray * array = @[@{@"arg0":json}];
    [HttpTools requestWithURL:BaseURL arrays:array method:Basemethod success:^(id JSON) {

         self.currentPage++;
    
     NSArray *array = JSON[@"reason"];
        for (NSDictionary *dic in array) {
    NSDictionary *dict = dic[@"attrs"];
    [_dataArray addObject:dict];
    
}

        [self.tableView footerEndRefreshing];
                [self.tableView reloadData];

    } failure:^(NSError *error) {
         [self.tableView footerEndRefreshing];
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  _dataArray.count;
}
#pragma mark UITableView dataScource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 static NSString *CellIdentifier = @"Cell";  

    jwTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  if (cell==nil) {  
        cell=[[jwTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    NSDictionary *dic = _dataArray [indexPath.row];
    cell.dict = dic;

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

NSDictionary *dic = _dataArray[indexPath.row];
    jwxqTableViewController *huodong = [[jwxqTableViewController alloc]init];
    huodong.action = @"jwqyzxMch";
    huodong.service = _service;
    huodong.string = [NSString stringWithFormat:@"%@详情",_string];
    huodong.id = dic[@"ID"];
    [self.navigationController pushViewController:huodong animated:YES];
//}
//if ([self.action isEqualToString:@"wsjdList"]) {
//    jiedaiController *huodong = [[jiedaiController alloc]init];
//    huodong.action = @"wsjdMch";
//    huodong.service = _service;
//    huodong.string = [NSString stringWithFormat:@"%@详情",_string];
//    huodong.id = dic[@"ID"];
//    [self.navigationController pushViewController:huodong animated:YES];
//}
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
